/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#define USING_LOG_PREFIX STORAGE
#include "ob_file_system_router.h"
#include "share/config/ob_server_config.h"
#include "observer/ob_server_struct.h"
#include "storage/ob_file_system_util.h"

namespace oceanbase {
using namespace common;
using namespace share;
namespace storage {

ObFileSystemRouter& ObFileSystemRouter::get_instance()
{
  static ObFileSystemRouter instance_;
  return instance_;
}

ObFileSystemRouter::ObFileSystemRouter()
{
  reset();
}

void ObFileSystemRouter::reset()
{
  data_dir_[0] = '\0';
  slog_dir_[0] = '\0';
  clog_dir_[0] = '\0';
  ilog_dir_[0] = '\0';
  clog_shm_dir_[0] = '\0';
  ilog_shm_dir_[0] = '\0';
  sstable_dir_[0] = '\0';
  is_inited_ = false;
}

int ObFileSystemRouter::init(const char* data_dir, const char* cluster_name, const int64_t cluster_id, const char* zone,
    const char* svr_ip_port_str)
{
  int ret = OB_SUCCESS;

  if (OB_UNLIKELY(is_inited_)) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else if (OB_ISNULL(data_dir) || OB_ISNULL(cluster_name) || OB_ISNULL(svr_ip_port_str) ||
             OB_UNLIKELY(cluster_id < 0) || OB_ISNULL(zone)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KP(data_dir), KP(cluster_name), KP(svr_ip_port_str), K(cluster_id), KP(zone));
  } else if (OB_FAIL(init_shm_file(data_dir))) {
    LOG_WARN("init shm file fail fail", K(ret), K(data_dir));
  } else if (OB_FAIL(init_local_dirs(data_dir))) {
    LOG_WARN("init local dir fail", K(ret), K(data_dir));
  } else {
    is_inited_ = true;
  }

  if (IS_NOT_INIT) {
    reset();
  }
  return ret;
}

int ObFileSystemRouter::init_local_dirs(const char* data_dir)
{
  int ret = OB_SUCCESS;
  int pret = 0;

  if (OB_SUCC(ret)) {
    pret = snprintf(data_dir_, MAX_PATH_SIZE, "%s", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct data dir fail", K(ret), K(data_dir));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(slog_dir_, MAX_PATH_SIZE, "%s/slog", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct slog path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(clog_dir_, MAX_PATH_SIZE, "%s/clog", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct clog path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(ilog_dir_, MAX_PATH_SIZE, "%s/ilog", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct ilog path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(clog_shm_dir_, MAX_PATH_SIZE, "%s/clog_shm", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct clog shm path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(ilog_shm_dir_, MAX_PATH_SIZE, "%s/ilog_shm", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct ilog shm path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(sstable_dir_, MAX_PATH_SIZE, "%s/sstable", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct sstable path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    LOG_INFO("succeed to construct local dir",
        K(data_dir_),
        K(slog_dir_),
        K(clog_dir_),
        K(ilog_dir_),
        K(clog_shm_dir_),
        K(ilog_shm_dir_),
        K(sstable_dir_));
  }
  return ret;
}

int ObFileSystemRouter::init_shm_file(const char* data_dir)
{
  int ret = OB_SUCCESS;
  int pret = 0;
  char cur_work_dir[MAX_PATH_SIZE] = {};

  if (OB_SUCC(ret)) {
    pret = snprintf(data_dir_, MAX_PATH_SIZE, "%s", data_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_WARN("copy data_dir fail", K(ret), K(data_dir));
    }
  }

  // mmap files
  if (OB_SUCC(ret)) {
    if (OB_ISNULL(::getcwd(cur_work_dir, sizeof(cur_work_dir)))) {
      ret = common::OB_IO_ERROR;
      LOG_ERROR("getcwd fail", K(ret), K(errno), KERRMSG);
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(clog_shm_dir_, MAX_PATH_SIZE, "%s/store/clog_shm", cur_work_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct clog shm path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    pret = snprintf(ilog_shm_dir_, MAX_PATH_SIZE, "%s/store/ilog_shm", cur_work_dir);
    if (pret < 0 || pret >= MAX_PATH_SIZE) {
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("construct ilog shm path fail", K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    LOG_INFO("succeed to construct shm file path",
        K(data_dir_),
        K(slog_dir_),
        K(clog_dir_),
        K(ilog_dir_),
        K(clog_shm_dir_),
        K(ilog_shm_dir_));
  }
  return ret;
}
}  // namespace storage
}  // namespace oceanbase
